package cn.itcast.service;

import cn.itcast.client.ConsumerClient;
import cn.itcast.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: taft
 * @Date: 2018-9-27 9:57
 */
@Service
public class UserService {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    ConsumerClient consumerClient;

    String baseUrl = "http://user-service/user/";


   // Logger logger = LoggerFactory.getLogger(UserService.class);

    //@HystrixCommand(fallbackMethod = "queryUserFallback")
    public User queryUserById(Long id){
        Long start = System.currentTimeMillis();
        //User user = restTemplate.getForObject(baseUrl+id,User.class);

        User user = consumerClient.queryUserById(id);

        Long end = System.currentTimeMillis();

       // logger.info("本次查询id：{}，耗时：{}",id,end-start);
        return user;
    }

    //降级回调方法的定义，方法名无所谓，但是参数和返回值类型必须一直
    public User queryUserFallback(Long id){
        User user = new User();
        user.setId(id);
        user.setName("抱歉，服务器找不到了，请稍后重试");
        return user;
    }
}
